package cn.newgrand.netcall.offlinefiletransfer;

import java.util.Random;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.packet.StreamInitiation;
import org.jivesoftware.spark.SparkManager;

import cn.newgrand.smackx.offlinefiletransfer.FileTransferNegotiator;
import cn.newgrand.smackx.packet.FileTransferCancel;


/**
 * A request to send a file recieved from another user.
 * 
 * @author Alexander Wenckus
 * 
 */
public class OfflineFileTransferRequest {

    public static final Random randomGenerator = new Random();
	
	private String fileName;
	private long fileSize;
	private String fileDesc;
	private String initiator;
	private String md5;
	private String server;
	private String proxyIP;
	private String proxyPort;
	private String sessionID;
	private String dataString;
	/**
	 * A recieve request is constructed from the Stream Initiation request
	 * received from the initator.
	 * 
	 * @param manager
	 *            The manager handling this file transfer
	 * 
	 * @param si
	 *            The Stream initiaton recieved from the initiator.
	 */
	public OfflineFileTransferRequest(String fileName,String fileSize,String fileDesc,String initiator,String md5,String form,String proxyIP,String proxyPort,String dataString) {
		this.fileName = fileName;
		this.fileSize = Long.parseLong(fileSize);
		this.fileDesc = fileDesc;
		this.initiator = initiator;
		this.md5 = md5;
		this.server = form;
		this.proxyIP = proxyIP;
		this.proxyPort = proxyPort;
		this.setDataString(dataString);
	}

	/**
	 * Returns the name of the file.
	 * 
	 * @return Returns the name of the file.
	 */
	public String getFileName() {
		return this.fileName;
	}

	/**
	 * Returns the size in bytes of the file.
	 * 
	 * @return Returns the size in bytes of the file.
	 */
	public long getFileSize() {
		return this.fileSize;
	}

	/**
	 * Returns the description of the file provided by the requestor.
	 * 
	 * @return Returns the description of the file provided by the requestor.
	 */
	public String getDescription() {
		return this.fileDesc;
	}

	/**
	 * Returns the fully-qualified jabber ID of the user that requested this
	 * file transfer.
	 * 
	 * @return Returns the fully-qualified jabber ID of the user that requested
	 *         this file transfer.
	 */
	public String getRequestor() {
		return this.initiator;
	}


	/**
	 * Accepts this file transfer and creates the incoming file transfer.
	 * 
	 * @return Returns the <b><i>IncomingFileTransfer</b></i> on which the
	 *         file transfer can be carried out.
	 */
	public OfflineIncomingFileTransfer accept() {
		return new OfflineIncomingFileTransfer(this);
	}

	/**
	 * Rejects the file transfer request.
	 */
	public void reject() {
//        IQ iqPacket = new IQ() {
//            public String getChildElementXML() {
//                return null;
//            }
//        };
//        iqPacket.setTo(server);
//        iqPacket.setFrom(SparkManager.getConnection().getUser());
//        iqPacket.setType(IQ.Type.ERROR);
//        iqPacket.setError(new XMPPError(XMPPError.Condition.forbidden));
//		SparkManager.getConnection().sendPacket(iqPacket);
		FileTransferCancel cancel = new FileTransferCancel();
		cancel.setTo(server);
		cancel.setFrom(SparkManager.getConnection().getUser());
		cancel.setType(IQ.Type.SET);
		cancel.setCancel(true);
		cancel.setFileName(fileName);
		cancel.setMd5(md5);
		cancel.setDataString(dataString);
		SparkManager.getConnection().sendPacket(cancel);
	}

	public String getMd5() {
		return md5;
	}

	public String getServer() {
		return server;
	}

	public String getProxyIP(){
		return proxyIP;
	}
	
	public String getProxyPort(){
		return proxyPort;
	}

	public String getSession() {
		// TODO Auto-generated method stub
		return sessionID;
	}
	
	public void setSession(String sessionID) {
		// TODO Auto-generated method stub
		 this.sessionID = sessionID;
	}

	public void setDataString(String dataString) {
		this.dataString = dataString;
	}

	public String getDataString() {
		return dataString;
	}
}

